<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\AuthGroupCreateRequest;
use Modules\Admin\Http\Requests\AuthGroupUpdateRequest;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Models\AuthGroup as AuthGroupModel;
use Modules\Admin\Models\AuthRule as AuthRuleModel;
use Modules\Admin\Http\Controllers\CommonController;
class AuthGroupController extends CommonController
{
	/**
	 * @name  列表
	 */
    public function index()
    {
		$status = AuthGroupModel::common_status();   	
        return view('admin::auth_group.index',compact('status'));
    }
	/**
	 * @name  列表数据
	 */
    public function data(Request $request)
    {
		$model = AuthGroupModel::query();
		$params = $request->all();
		$model = query_condition($model,$params,'title');	
        $res = $model->orderBy('id','desc') 
				->paginate($params['limit'])
				->toArray(); 
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg') ,'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    { 
		return view('admin::auth_group.create');
    }
	/**
	 * @name  添加方法数据处理
	 */
    public function store(AuthGroupCreateRequest $request)
    {
		return (new AuthGroupModel())->common_create($request->all(),route('admin.auth_group.index'));
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$info = AuthGroupModel::find($id);
		return view('admin::auth_group.edit',compact('info'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(AuthGroupUpdateRequest $request,$id)
    {
		return (new AuthGroupModel())->common_update($request->all(),$id,route('admin.auth_group.index')); 
    }
	/**
	 * @name  配置规则页面
	 */
    public function access($id)
    {
		$rules = (new AuthGroupModel())->where(['id'=>$id])->value('rules');
		$arr = (new AuthRuleModel())->auth_list($rules);
		$arr[] = array(
				"id"=>0,
				"pid"=>0,
				"title"=>"全部",
				"open"=>true
			);              
		$data = json_encode($arr,true);       
		return view('admin::auth_group.access',['data'=>$data,'id'=>$id]);
    }
	/**
	 * @name  配置规则数据处理
	 */
    public function access_update(Request $request,$id)
    {
		$data = $request->all();
		if(!$data['rules']){
			return response()->json([
				'status'=>0,
				'error' => '请选择配置项！',
			],201);
		}
		return (new AuthGroupModel())->common_update($data,$id,route('admin.auth_group.index'),'配置成功！','配置失败！'); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new AuthGroupModel())->common_status_update($request->all(),$id,route('admin.auth_group.index')); 
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {
		return (new AuthGroupModel())->common_destroy($request->get('id'),route('admin.auth_group.index'));  
    }
	
	
	
	
	
	
}
